<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_SESSION['os']!='windows'){
	exi('','','jy_merch/manage.index/');
}
if($_RQ['merchid']){
	$merchid=$this->checklogin($_RQ['merchid']);
}elseif($_SESSION['merchid']){
	$merchid=$this->checklogin();
}else{
	if(!$_SESSION['uid']){
		exi('','','/login/');
	}
	$merchid=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	if(!$merchid){
		$_SESSION['uid']=0;
		exi('','error',custom_url('jy_lianmerch/login/',true));
		$info=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('mobile','nickname','realname'));
		pdo_insert('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'jointime'=>time(),'realname'=>$info['realname'],'mobile'=>$info['mobile'],'merchname'=>$info['nickname']));
		$merchid=pdo_insertid();
	}
	$_SESSION['merchid']=$_SESSION['merchid']?:$merchid;
}
if($_RQ['op']=='main'){
	$_SESSION['logout_forward']='jy_lianmerch/login/';
	$return['item']=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$return['item']['logo']=tomedia($return['item']['logo']);
	$return['isopenvoice']=$return['item']['isopenvoice'];
	$return['merchs']=pdo_getall('jy_merch_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['plugin_icon']=$return['item']['logo'];
	$this->session('plugin_icon',$return['item']['logo']);
	$avatar=pdo_getcolumn('core_members',array('id'=>$return['item']['mid']),'avatar');
	$return['item']['logo']=$return['item']['logo']?$return['item']['logo']:$avatar;
	$return['item']['url']=custom_url('jy_merch/merch//id='.$merchid,true);
	$return['item']['payurl']=custom_url('jy_merch/member.recharge//merchid='.$merchid,true);
	//计算待结算金额
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>3);
	$ids=pdo_getall('jy_weishop_order',$params,'id','id');
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'clearstatus'=>0);
	if($ids){
			$ids=array_keys($ids);
			$params['orderid']=$ids;
	}
	$order_goods=pdo_getall('jy_weishop_order_goods',$params,array('id','orderid','total_price','merchid','goodsid','optionid','fee_desc'));
	$clearfee0=0;
	$commission_money0=0;
	foreach($order_goods as &$gv){
				$order=pdo_get('jy_weishop_order',array('id'=>$gv['orderid']),array('ordersn','createtime','dispatchprice','peisong_id'));
				$gv['ordersn']=$order['ordersn'];
				$gv['createtime']=$order['createtime'];
				$gv['ordergoodsid']=$gv['id'];
				$gv['id']=$gv['orderid'];
				//商品规格
				if($gv['optionid']){
					$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$optionid),'specs');
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
					}
				}
				if(!$gv['fee_desc']){
					model('jy_weishop/order')->setfee_desc($gv['orderid']);
					$gv['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$gv['id']),'total_price');
				}
				$total_price=$gv['total_price'];
				if($order['peisong_id']){
					$total_price -=$order['dispatchprice'];
				}
				$clearfee0 +=$total_price;
				//计算佣金
				if($this->plugin_exists('jy_commission')){
						$gv['commission_money']=model('jy_commission/commission')->get_goodsfee($gv['goodsid'],$gv['orderid'],$gv['ordergoodsid']);
						$commission_money0 +=$gv['commission_money'];
				}
	}
	$return['clearfee0']=price($clearfee0);
	$return['commission_money0']=price($commission_money0);
	$return['params']=array('daytype'=>0,'time'=>'','qushi'=>0,'qushitype'=>0);
	if($_SESSION['wxapp_id']){
		$tmpid=cfc('wxapp_api')->get_tpl_template_id('1538',[7,1,2,3],'pay2seller','订单支付成功通知卖方','jy_weishop');
		if($tmpid){
			$tmplIds[]=$tmpid;
		}
		$return['tmplIds']=$tmplIds;
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>0);
	$params['status']=1;
	$return['status1']=pdo_count('jy_weishop_order',$params);
	$params['status']=0;
	$return['ispay']=pdo_count('jy_weishop_order',$params);
	$params['status']=2;
	$return['status2']=pdo_count('jy_weishop_order',$params);
	$return['goods']=pdo_count('jy_weishop_goods',array('status <>'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
	$return['delgoods']=pdo_count('jy_weishop_goods',array('status'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
	$return['verifygoodsnum']=pdo_count('jy_weishop_verifygoods_log',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'salerid'=>$_SESSION['uid']),'id');
	$return['verifynum']=pdo_count('jy_weishop_order_verify',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clerk_id'=>$_SESSION['uid']),'id');
	$return['selfverifynum']=pdo_count('jy_weishop_order_self',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clerk_id'=>$_SESSION['uid']),'id');
	set_html('title','商户后台');
	exi($return);
}
if($_RQ['op']=='getdata'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	//创建时间
	if($_RQ['tstart'] && $_RQ['tend']){
		$params['createtime between']=array($_RQ['tstart'],$_RQ['tend']);
	}else{
		$daytype=intval($_RQ['daytype']);
		if($daytype==0){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$params['createtime >']=$daytime;
		}elseif($daytype==1){
			$yesdaytime = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$params['createtime between']=array($yesdaytime,$daytime);
		}elseif($daytype==2){
			$day7time = mktime(0,0,0,date('m'),date('d')-7,date('Y'));
			$params['createtime >']=$day7time;
		}elseif($daytype==3){
			
		}
	}
	//print_r($params);exit;
	$params['ispay']=1;
	$data['paybi']=pdo_count('jy_weishop_order',$params);
	$data['payren']=pdo_count('jy_weishop_order',$params);
	$data['payyuan']=pdo_getsum('jy_weishop_order',$params,'price');
	$data['payjian']=pdo_getsum('jy_weishop_order_goods',$params,'total');
	$data['shoubi']=pdo_count('jy_weishop_order',$params);
	$data['shouren']=pdo_count('jy_weishop_order',$params);
	$data['shouyuan']=pdo_getsum('jy_weishop_order',$params,'price');
	$data['shoujian']=pdo_getsum('jy_weishop_order_goods',$params,'total');
	$return['params']=$params;
	if($_SESSION['os']=='windows'){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$access['status_day']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime));
		$access['status_all']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid']));
		$access['status1_day']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3),'createtime >'=>$daytime));
		$access['status1_all']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3)));
		//月份
		for($i=0;$i<12;$i++){
					if($i==0){
						$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
						$yueend = time();
						
					}else{
						$yuestart = mktime(0,0,0,date('m')-$i,1,date('Y'));
						$yueend = mktime(0,0,0,date('m')-$i+1,1,date('Y'));
					}
					$params=array('uniacid'=>$_SESSION['uniacid']);
					$params['createtime between']=array($yuestart,$yueend);
					$month12[$i] = date('Y-m',strtotime('-'.$i.' month'));
					//$monthaccess[]=pdo_count('core_access_log',$params);
					$accessline[$i]=pdo_count('jy_weishop_order',$params);
					//$monthcredit2[]=pdo_getsum('core_members',$params,'credit2');
		}
		$accessline=array_reverse($accessline);
		$month12=array_reverse($month12);
		$htmls=array(
					array('type'=>'data','tpltype'=>4,'one_num'=>4,'title'=>'今日/总订单数据指标','datas'=>array(
						array('title'=>'今日/总订单数','num'=>2,'data'=>array($access['status_day'],$access['status_all']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'今日/总成交数','bgcolor'=>'#009933','num'=>2,'data'=>array($access['status1_day'],$access['status1_all']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'待发货/收货数','bgcolor'=>'#3366FF','num'=>2,'data'=>array($return['status1'],$return['status2']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'今日/总订单数','bgcolor'=>'#FF3300','num'=>2,'data'=>array($access['pc_day'],$access['status_all']),'datatitle'=>array('客户数','申请数')),
					)),
					array('type'=>'echarts_arealine','ecid'=>'mobile','title'=>'月度订单数据','datas'=>array(
						'datas'=>array(
							array('name'=>'订单数','value'=>$accessline),
						),
						'xdatas'=>$month12,
					)),
		);	
		//$return['htmls']=$htmls;
	}
	$return['data']=$data;
	exi($return);
}